using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using Sirenix.Serialization;

namespace BTEditor
{
    [BTTask]
    public class Action : Task
    {
        public enum ActionType
        {
            Action,
            Func_Bool,
            Func_Result_Bool,
            Func_Result_Request
        }

        public override Type GetNodeType()
        {
            return typeof(NPBehave.Action);
        }

        public override ErrorType GetIsVaild()
        {
            return boolFunc == null ? ErrorType.Error : ErrorType.None;
        }

        [Title("方法类型")] [EnumToggleButtons] [HideLabel] [OdinSerialize] [OnValueChanged("onActionTypeChange")]
        public ActionType actionType = ActionType.Action;

        [ValueDropdown("ValuesFunction", DropdownTitle = "方法选择", DropdownHeight = 500,
            DisableListAddButtonBehaviour = true)]
        [HideLabel]
        [Title("方法")]
        [ShowInInspector]
        [BTValue(DealType.ActionToChild)]
        [OdinSerialize]
        public ActionBase boolFunc;

        private void onActionTypeChange()
        {
            boolFunc = null;
        }

        private IEnumerable ValuesFunction()
        {
            List<Type> types = GetType().Assembly.GetTypes().ToList();
            Type filtType;
            switch (actionType)
            {
                case ActionType.Action:
                    filtType = typeof(NormalAction);
                    break;
                case ActionType.Func_Bool:
                    filtType = typeof(BoolFunc);
                    break;
                case ActionType.Func_Result_Bool:
                    filtType = typeof(Result_Bool_Func);
                    break;
                case ActionType.Func_Result_Request:
                    filtType = typeof(Result_Request_Func);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return types.Where(x => x.IsSubclassOf(filtType))
                .Select(x => Activator.CreateInstance(x))
                .Select(x => new ValueDropdownItem(x.ToString(), x));
        }
    }
}